package com.mdinic.blogovi.front.page.follow;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.AttributeModifier;
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.mdinic.blogovi.front.page.BSession;
import com.mdinic.blogovi.model.Person;
import com.mdinic.blogovi.service.PersonService;

public class FollowingPanel extends Panel {

    private static final Person NO_FILTER;

    private static final String SELECTED = "background-color: #FFFFFF ; color: #000000; font-size: medium;";

    static {
        NO_FILTER = new Person();
        NO_FILTER.setNickname("Svi prijatelji");
    }

    private static final long serialVersionUID = 1L;

    @SpringBean
    private transient PersonService personService;

    public FollowingPanel(String id) {
        super(id);

        List<Person> blogTypes = new ArrayList<Person>();
        blogTypes.add(NO_FILTER);
        List<Person> following = personService.getFollowing(BSession.get().getPerson());
        blogTypes.addAll(following);
        final StringBuilder sb = new StringBuilder();

        final Person followFilter = BSession.get().getFollowFilter();

        ListView<Person> types = new ListView<Person>("types", blogTypes) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(final ListItem<Person> item) {

                if (followFilter == null && item.getModelObject().getId() == null) {
                    sb.append(SELECTED);
                } else if (followFilter != null && followFilter.getId().equals(item.getModelObject().getId())) {
                    sb.append(SELECTED);
                } else {
                    sb.append("color: #FFFFFF; background-color: #000000;");
                }

                Link<Void> link = new Link<Void>("link") {

                    private static final long serialVersionUID = 1L;

                    @Override
                    public void onClick() {
                        if (item.getModelObject().getId() == null) {
                            BSession.get().setFollowFilter(null);
                        } else {
                            BSession.get().setFollowFilter(item.getModelObject());
                        }
                        setResponsePage(getPage().getClass());
                    }
                };

                link.add(new AttributeAppender("style", sb.toString()));
                item.add(link.add(new Label("link_text", item.getModelObject().getNickname())));
            }
        };

        add(types);

        add(new Link<Void>("unreadLink") {

            private static final long serialVersionUID = 1L;

            @Override
            public void onClick() {
                BSession.get().setFollowFilterRead(false);
                setResponsePage(FollowLinksPage.class);
            }

        }.add(new AttributeModifier("class", BSession.get().isFollowFilterRead() ? "follow" : "follow_active")));

        add(new Link<Void>("readLink") {

            private static final long serialVersionUID = 1L;

            @Override
            public void onClick() {
                BSession.get().setFollowFilterRead(true);
                setResponsePage(FollowLinksPage.class);
            }

        }.add(new AttributeModifier("class", BSession.get().isFollowFilterRead() ? "follow_active" : "follow")));
    }
}
